package service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.StudentVo;
import entity.classes;
import entity.student;
import mapper.ClassesMapper;
import mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import service.StudentService;
import util.myBatisUtil;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<student> findALL() {
        try (SqlSession sqlSession = myBatisUtil.getSS(true);){
            //查询所有学生
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
            List<student> students = mapper.findAll();
            //遍历学生列表,根据班级编号,找到班级名称
            for (student student : students) {
               //获取班级编号
                Integer cid = student.getCid();
                classes classes=classesMapper.findById(cid);
                student.setCname(classes.getCname());
            }
            return students;
        }
    }

    @Override
    public PageResult<student> findByPage(int page, int pageSize) {
        //开始分页
        PageHelper.startPage(page,pageSize);
        //开始查询,对所有记录进行分页
        List<student> students = this.findALL();
        Page<student> studentPage= (Page<student>) students;
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }

    @Override
    public PageResult<student> search(int page, int pageSize, StudentVo studentVo) {
//开始分页
        PageHelper.startPage(page,pageSize);
        //开始查询,对所有记录进行分页
        List<student> students = this.findByWords(studentVo);
        Page<student> studentPage= (Page<student>) students;
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());    }

    @Override
    public List<student> findByWords(StudentVo studentVo) {
        try (SqlSession sqlSession = myBatisUtil.getSS(true);) {
            //得到所有的mapper
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
            //根据条件查询学生
            List<student> students = mapper.findByWords(studentVo);
            //遍历学生列表,根据班级编号,找到班级名称
            for (student student : students) {
                //获取班级编号
                Integer cid = student.getCid();
                classes classes=classesMapper.findById(cid);
                student.setCname(classes.getCname());
            }
            return students;
        }
    }

    //保存学生
    @Override
    public void save(student student) {
        try (SqlSession sqlSession = myBatisUtil.getSS(true);) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.insert(student);
        }
    }
    //修改学生
    @Override
    public void update(student student) {
        try (SqlSession sqlSession = myBatisUtil.getSS(true);) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.update(student);
        }
    }
//删除学生
    @Override
    public void delete(List<String> ids) {
        try (SqlSession sqlSession = myBatisUtil.getSS(true);) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.delete(ids);
        }
    }
}

